package cn.sxt.userscenter.head.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSONObject;

import cn.sxt.userscenter.common.controller.BaseController;
import cn.sxt.userscenter.common.util.ConstatFinalUtil;
import cn.sxt.userscenter.common.util.PageInfoUtil;
import cn.sxt.userscenter.users.pojo.AAdmins;
import cn.sxt.userscenter.users.pojo.AUsers;
import cn.sxt.userscenter.users.pojo.AUsersEnum;
import cn.sxt.userscenter.users.pojo.AUsersExtend;
import cn.sxt.userscenter.users.service.IUsersService;

/**
 * 不需要登录的操作
 * @author PC-CZ
 *
 */
@Controller
public class NoLoginController extends BaseController{
	@Resource
	private IUsersService usersService;
	/**
	 * 管理员列表页面
	 * @param req
	 * @return
	 */
	@RequestMapping("/adminsList")
	public String adminsList(HttpServletRequest req){
		ConstatFinalUtil.LOGGER.info("----adminsList----");
		
		PageInfoUtil pageInfoUtil = new PageInfoUtil();
		//按照id查询
		Map<String,Object> condMap = new HashMap<String,Object>();
		List<AAdmins> adminsList = this.usersService.findCondListAdmins(pageInfoUtil, condMap);
		//储存结果
		req.setAttribute("adminsList", adminsList);
		req.setAttribute("pageInfoUtil", pageInfoUtil);
		
		return "/head/adminsList";
		
	}
	/**
	 * 打开登录页面
	 */
	@RequestMapping("/login")
	public String login(){
		ConstatFinalUtil.LOGGER.info("-------login------");
		return "/head/login";
	}
	
	/**
	 * 打开登录页面
	 */
	@RequestMapping("/loginSubmit")
	public String loginSubmit(HttpServletRequest request,HttpSession session,String id){
		
		
		ConstatFinalUtil.LOGGER.info("-------loginSubmit------");
		String email = request.getParameter("email");
		String password = request.getParameter("password");
		
		//获取页面的用户填写的验证码
		String code = request.getParameter("code");
		
		//从session中获取 系统自动生成的 验证码
		String sessCode = session.getAttribute("rand") + "";
		//放置用户也已灌水   将验证码在session中移除
		session.removeAttribute("rand");
		
		
		
		//根据用户名查询用户
		Map<String,Object> condMap = new HashMap<String,Object>();
		condMap.put("email", email);
		AUsers users = this.usersService.findOneUsers(condMap);
		if(sessCode != null &&
				sessCode.equalsIgnoreCase(code)){//存在疑问  sessCode不可能为空  是不是可以去掉 sessCode不为null的判断
			//开始验证登录
			if(users!=null){
				//邮箱存在
				if(this.encryptUtil.decode(password, users.getPassword())){
					//密码正确
					if(users.getStatus() == AUsersEnum.STATUS_ENABLED.getStatus()){
						//账户为启用状态
						//登录成功
						//放入session
						session.setAttribute("users", users);
						session.setAttribute("lastLoginTime", users.getLastLoginTime());
						
						users.setLastLoginTime(new Date());
						this.usersService.updateOneUsersService(users);
						
						//客户端跳转到登录成功的页面
						return "redirect:/head/users/main.htm";
					}else{
						//账户被禁用
						ConstatFinalUtil.LOGGER.info("---账户被禁用---");
						this.info = "账户被禁用";
					}
				}else{
					//密码不正确
					ConstatFinalUtil.LOGGER.info("---密码不正确---");
					this.info = "密码不正确";
				}
				request.setAttribute("email", email);
			}else{
				//邮箱不存在
				ConstatFinalUtil.LOGGER.info("---邮箱不存在---");
				this.info = "邮箱不存在";
			}
		}else{
			//验证码不正确
			ConstatFinalUtil.LOGGER.info("---验证码不正确---");
			this.info = "验证码不正确";
		}
		request.setAttribute("info", info);
		
//		//模拟成功登录
//		if("1".equalsIgnoreCase(id)){
//			//登陆成功
//			//客户端跳转到登陆成功的页面
//			return "redirect:/head/users/mian.htm";
//		}
		
		
		return this.login();
	}
	/**
	 * 打开注册页面
	 * @return
	 */
	@RequestMapping("/register")
	public String register(){
		ConstatFinalUtil.LOGGER.info("---regsiter---");
		return "/head/register";
	}
	
	
	/**
	 * 提交注册页面
	 * @return
	 */
	@RequestMapping("/registerSubmit")
	public String registerSubmit(HttpServletRequest request,HttpSession session){
		ConstatFinalUtil.LOGGER.info("---registerSubmit---");
		
		String email = request.getParameter("email");
		String password = request.getParameter("password");
		
		//获取页面的用户填写的验证码
		String code = request.getParameter("code");
		
		//从session中获取 系统自动生成的 验证码
		String sessCode = session.getAttribute("rand") + "";
		//放置用户也已灌水   将验证码在session中移除
		session.removeAttribute("rand");
		

		
		if(sessCode != null &&
			sessCode.equalsIgnoreCase(code)){//存在疑问  sessCode不可能为空  是不是可以去掉 sessCode不为null的判断
			//存入一条用户信息
			AUsers users = new AUsers();
			users.setEmail(email);
			users.setPassword(this.encryptUtil.encode(password));
			//在用户注册的时候,将用户的状态设置为启用状态
			users.setStatus(AUsersEnum.STATUS_ENABLED.getStatus());
			
			
			users.setCreateTime(new Date());
			users.setUpdateTime(new Date());
			users.setLastLoginTime(new Date());
			
			JSONObject resultJSON = this.usersService.saveUsersService(users);
			this.info = resultJSON.getString("info");

		}else{
			//验证码不正确
			ConstatFinalUtil.LOGGER.info("---验证码不正确---");
			this.info = "验证码不正确";	
		}
		
		request.setAttribute("info", info);
		
		return this.register();
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
}
